package XMFramework.Core
{
	import XMFramework.Interface.ILocalization;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import mx.utils.StringUtil;

	public class SLocalization implements ILocalization
	{
		internal var totalByte:int				=	0;
		internal var loadedByte:int				=	0;
		internal var isInitCompleted:Boolean	=	false; 
		
		private var onResultFunc:Function		=	null;
		private var onStepLoadFunc:Function		=	null;
		
		private var _languageTable : Object		=	new Object();		
		private var _configData : Object;		
		private var _dataURL : String			=	"unknown";
		private var _dataName : String			=	"unknown";
				
		public function downloadData( onResult:Function, onStepLoad:Function ) : void
		{			
			onResultFunc			=	onResult;
			onStepLoadFunc			=	onStepLoad;
			
			var loader : URLLoader	=	new URLLoader();
			loader.dataFormat		=	URLLoaderDataFormat.BINARY;	
			
			loader.addEventListener( Event.COMPLETE, onDownloadComplete );
			loader.addEventListener( IOErrorEvent.IO_ERROR, onDownloadFault );
			loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
			loader.addEventListener( ProgressEvent.PROGRESS, onLoadPercentage );
			loader.load( new URLRequest( _dataURL ) );		
			
			trace( "SLocalization::downloadData started: " + _dataURL );
		}	
		
		private function onDownloadComplete( event : Event ) : void
		{
			parserData( event.target.data );
			isInitCompleted	=	true;
			
			if( onResultFunc != null )
				onResultFunc.call( null, true );
			
			trace( "SLocalization::onDownloadComplete succeeded: " + _dataURL );
		}
		
		private function onDownloadFault( evt : IOErrorEvent ) : void
		{
			trace("SLocalization download data failure: " + evt.text );
		
			downloadData( onResultFunc, onStepLoadFunc );
		}
		
		private function securityErrorHandler( evt:SecurityErrorEvent ) : void 
		{
			trace("SLocalization download data failure: " + evt.text );
			
			downloadData( onResultFunc, onStepLoadFunc );
		}
		
		private function onLoadPercentage( evt:ProgressEvent ) : void
		{
			totalByte 	=	evt.bytesTotal;
			loadedByte	=	evt.bytesLoaded;
			
			if( onStepLoadFunc != null )
				onStepLoadFunc.call( null, _dataName, totalByte, loadedByte );
		}
		
		private function parserData( data : String ) : void
		{
			var start : int, seek:int;
			var bigarr : Array		=	data.split( "\r\n" );
			
			for each( var line : String in bigarr )
			{
				if( ! line || line.length == 0)
					continue;
				
				var id : String		=	"";
				var data : String	=	"";
				
				// Check this is comment string
				if( line.charAt( 0 ) != ';' )
				{
					for( start=0; start<line.length; start++ )
						if( ! StringUtil.isWhitespace( line.charAt(start) ) )
							break;
					
					/////////////////////////					
					for( seek=start; seek<line.length; seek++ )
						if( line.charAt(seek) == '=' )
							break;
					
					id	=	line.substring( start, seek );					
					id	=	StringUtil.trim( id );
					
					///////////////////////
					if( seek + 1 < line.length )
						data	=	line.substring( seek + 1 );
					else
						data	=	"";
					
					//////////////////////
					if( id.length != 0 )
						_languageTable[id]	=	data;
				}
			}						
		}
		
		/////////////////////////////////////////////
		// Public methods
					
		public function SLocalization( mediaPath:String, config : Object )
		{
			_configData	=	config;
			
			if( _configData.hasOwnProperty( "default" ) )
				_dataName	=	_configData["default"];
			
			if( _configData.hasOwnProperty( "url" ) )
				_dataURL	=	mediaPath + _configData["url"][_dataName];
		}		
			
		public function loadRegion( name : String ) : void
		{
			
		}		
					
		public function getString( id : String ) : String
		{
			id	=	StringUtil.trim( id );
			var str : String	=	id;
			if( _languageTable.hasOwnProperty( id ) )
				str	=	new String( _languageTable[id] );
			
			return str;
		}		
		
		public function getStringFormat( id : String, ...params ) : String
		{
			id	=	StringUtil.trim( id );
			var str : String	=	id;
			if( _languageTable.hasOwnProperty( id ) )
				str	=	StringUtil.substitute( _languageTable[id], params );
			
			return str;
		}
	}
}